package com.jiazhong.basic.borrow.dao;

import com.jiazhong.basic.borrow.bean.User;
import com.jiazhong.basic.borrow.db.DBManager;
import lombok.SneakyThrows;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDAO {

    private Connection conn;
    private PreparedStatement ps;
    private ResultSet rs;
    @SneakyThrows
    public User login(String username,String password){
        conn = DBManager.getDruidConnection();
        String sql = "select * from `user` where username=? and `password`=md5(?)";
        ps = conn.prepareStatement(sql);
        ps.setString(1,username);
        ps.setString(2,password);
        rs = ps.executeQuery();
        if(rs.next()){
            User user = new User();
            user.setId(rs.getInt(1));
            user.setUsername(rs.getString(2));
            user.setPassword(rs.getString(3));
            user.setGender(rs.getString(4));
            return user;
        }
        return null;
    }
}
